unit Unit15; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  DBGrids, Menus, Unit1, Unit16, Unit17, LR_Class, LR_DBSet, Unit61;

type

  { TForm15 }

  TForm15 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem11Click(Sender: TObject);
    procedure MenuItem13Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid(pordeuda : string);
    procedure MenuItem12Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
  private
    nombre : string;
  public
    { public declarations }
  end; 

var
  Form15: TForm15; 

implementation

{ TForm15 }

procedure TForm15.ordenar_grid(pordeuda : string);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if pordeuda = 'si' then
    SQLQuery1.SQL.Text := 'select * from vw_cliente where saldo > 0 order by saldo desc'
  else
  if pordeuda = 'score' then
    SQLQuery1.SQL.Text := 'select * from vw_cliente where saldo > 0 order by score desc'
  else
    SQLQuery1.SQL.Text := 'select * from vw_cliente where cln_nombre ilike ''%' + nombre
      + '%'' order by cln_nombre';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'CUIT';
  DBGrid1.Columns[3].Title.Caption := 'Responsabilidad I.V.A.';
  DBGrid1.Columns[4].Title.Caption := 'Dirección';
  DBGrid1.Columns[5].Title.Caption := 'Región';
  DBGrid1.Columns[6].Title.Caption := 'e-mail';
  DBGrid1.Columns[7].Title.Caption := 'Teléfono';
  DBGrid1.Columns[8].Title.Caption := 'Saldo Cta. Cte.';
  DBGrid1.Columns[9].Title.Caption := 'Score';

  DBGrid1.Columns[0].Width := 50;
  DBGrid1.Columns[1].Width := 130;
  DBGrid1.Columns[2].Width := 80;
  DBGrid1.Columns[3].Width := 130;
  DBGrid1.Columns[4].Width := 200;
  DBGrid1.Columns[5].Width := 90;
  DBGrid1.Columns[6].Width := 210;
  DBGrid1.Columns[7].Width := 115;
  DBGrid1.Columns[8].Width := 85;
  DBGrid1.Columns[9].Width := 70;

  DBGrid1.Columns[2].Visible := false;
  DBGrid1.Columns[3].Visible := false;
  
  TColumn(DBGrid1.Columns[8]).DisplayFormat := '$ ####0.00';
end;

procedure TForm15.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  
  nombre := '%';

  ordenar_grid('no');
end;

procedure TForm15.MenuItem10Click(Sender: TObject);
begin
  Form61.ShowModal;
  FormShow;
end;

procedure TForm15.MenuItem11Click(Sender: TObject);
begin
  ordenar_grid('score');
end;

procedure TForm15.MenuItem13Click(Sender: TObject);
begin
  frReport1.LoadFromFile('listado_clientes.lrf');
  frReport1.ShowReport;
end;

procedure TForm15.MenuItem9Click(Sender: TObject);
begin
  ordenar_grid('si');
end;

procedure TForm15.MenuItem12Click(Sender: TObject);
begin
  FormShow;
end;

procedure TForm15.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm15.DBGrid1CellClick(Column: TColumn);
begin
  Form17.codigo := SQLQuery1.FieldByName('cln_cdg').AsString;
  Form17.ShowModal;
  FormShow;
end;

procedure TForm15.MenuItem2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm15.MenuItem4Click(Sender: TObject);
begin
  Form16.ShowModal;
  FormShow;
end;

procedure TForm15.MenuItem5Click(Sender: TObject);
var
  codigo : String;
  QueryString : String;

begin
  codigo := InputBox('Baja de Cliente', 'Ingrese el código del cliente a eliminar:', '');

  if codigo <> '' then
  begin
    QueryString := 'delete from cliente where cln_cdg = ' + codigo;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('BAJA EXITOSA');
  end;

  FormShow;
end;

procedure TForm15.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click sobre el cliente a modificar');
end;

procedure TForm15.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro de Clientes por Nombre:', 'Ingrese parte del nombre del cliente:', '');
  
  if nombre <> '' then
    ordenar_grid('no');
end;

initialization
  {$I unit15.lrs}

end.

